<?php

namespace app\index\controller;

use app\common\model\Node;
use think\Controller;
use think\Db;

class Base extends Controller
{
    /**
     * page当前页码
     * @var string
     */
    public $page = '';
    /**
     * 每页显示多少条
     * @var string
     */
    public $size = '';
    /**
     * 查询条件的起始值
     * @var int
     */
    public $from = 0;
    /**
     * 管理员信息
     * @var int
     */
    public $user = array();
    /**
     * 路由
     * @var int
     */
    public $url = '';

    public function _initialize()
    {
        $param = input('param.');
        $module     = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url        = $module."/".$controller."/".$action;
        $this->url = $url;
            if (empty($param['token'])) {
                json(array("code" => 10002, "msg" => "请重新登录！"))->send();
                die;
            }
            $hasUser = Db::name('manager_password')->field('id,mid,comid,update_time,dutyid')->where('token', $param['token'])->find();
            if(empty($hasUser)){
                json(array("code" => 10002, "msg" => "请重新登录！！"))->send();
                die;
            }
//            $hasUser['dutyid'] = $duty['dutyid'];

            //判断会话是否过期
            if (time() - $hasUser['update_time'] > config('index.token_expire')) {
                json(array("code" => 10003, "msg" => "请您重新登录！"))->send();
                die;
            }
            $company = Db::name('company')->field('manager_id')->where(['id' => $hasUser['comid']])->find();
            $auth = new \com\Auth();
            if(!in_array($url, config('index.permission_authlist'))) {
                if ($company['manager_id'] != $hasUser['mid']) {
                    if (!in_array($hasUser['dutyid'], array(0, 2))) {
                        if (!$auth->check($url, $hasUser['id'])) {
                            json(array("code" => 10004, "msg" => "抱歉，您没有操作权限！"))->send();
                            die;
                        }
                    }
                }
            }

        if($hasUser['dutyid'] == 0){
            $hasUser['dutyid'] = 2;
        }
        $this->user = $hasUser;
    }

    /**
     * [getPageAndSize  获取分页page size 内容]
     * @author [C。]
     * @param $data
     */
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty(config('list_rows')) ? config('list_rows') : config('paginate.list_rows');//后台设置显示条数、配置文件显示条数
        $this->from = ($this->page - 1) * $this->size;
    }
}